<?php
/**
 * @title 994. 腐烂的橘子
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $grid
     * @return Integer
     */
    function orangesRotting($grid) {
        /**
         * @since 2024-01-03 新鲜橘子数量
         */
        $freshCount = 0;

        /**
         * @since 2024-01-03 腐烂橘子的坐标集合
         */
        $rottenArray = [];

        /**
         * @since 2024-01-03 行数和列数
         */
        $row = count($grid);
        $col = count($grid[0]);

        /**
         * @since 2024-01-03 遍历数组，统计新鲜橘子数量，腐烂橘子坐标
         */
        for($r=0; $r<$row; $r++){
            for($c=0; $c<$col; $c++){
                if($grid[$r][$c] === 1){
                    $freshCount++;
                } elseif($grid[$r][$c] === 2){
                    $rottenArray[] = [$r, $c];
                } else {}
            }
        }

        /**
         * @since 2024-01-03 模拟腐烂橘子变化
         */
        $minute = 0;
        while($rottenArray AND $freshCount>0){
            /**
             * @since 2024-01-03 下次腐烂的橘子
             */
            $nextRottenArray = [];

            /**
             * @since 2024-01-03 本轮腐烂的橘子
             */
            foreach ($rottenArray as list($r, $c)){
                /**
                 * @since 2024-01-03 腐烂的4个方向
                 */
                $moveArray = [
                    [$r-1, $c],
                    [$r+1, $c],
                    [$r, $c-1],
                    [$r, $c+1],
                ];

                /**
                 * @since 2024-01-03 遍历4个方向找新鲜的橘子
                 */
                foreach ($moveArray as list($nextR, $nextC)){
                    /**
                     * @since 2024-01-03 存在且为新鲜橘子
                     */
                    if(isset($grid[$nextR][$nextC]) AND $grid[$nextR][$nextC] == 1){
                        $nextRottenArray[] = [$nextR, $nextC];
                        $grid[$nextR][$nextC] = 2;
                        $freshCount--;
                    } else {}
                }
            }

            /**
             * @since 2024-01-03 下次遍历
             */
            $rottenArray = $nextRottenArray;
            $minute++;
        }

        /**
         * @return Integer
         */
        return $freshCount === 0 ? $minute : -1;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.03_8.php
 */
$datas = [
    [[[2,1,1],[1,1,0],[0,1,1]]], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->orangesRotting(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
